<?
$lIsMod=1; 
require("../../admheader.php"); 

writeCommonAdminTable("Regions module",1);

$lTableRegion=$set_table_prefix . "regions";


// ## Set up tables
$lSQL="CREATE TABLE IF NOT EXISTS $lTableRegion (
`reg_id` INT NOT NULL AUTO_INCREMENT ,
`reg_name` VARCHAR( 255 ) ,
`reg_father` INT,
PRIMARY KEY ( `reg_id` ))";
q($lSQL);


if (getParam("add",""))
{
	q("insert into $lTableRegion (reg_name,reg_father) values ('".getParam("reg_name","")."',".getParam("reg_father",0).")");
}

if (getParam("del",""))
{
	if (getParam("reg_father","")>0)
	{
	$res=q("select * from $lTableRegion WHERE reg_father=" . getParam("reg_father",""));
	$lNum=mysql_num_rows($res);
	
	if ($lNum==0)
	{	
		q("delete from $lTableRegion where reg_id=" . getParam("reg_father",""));
		
		echo "<p class='bg'>Region deleted successfully!</p>";
	}
	else 
		echo "<p class='bg'>ERROR: You need to delete any subs first!</p>";
	}
}

$lSelected=getParam("reg_father","");

function untreeRegion($parent, $level, $input)
{
	global $lTableRegion,$lSelected;
	
	$parentsql = q("select reg_id, reg_name, reg_father from $lTableRegion where reg_father=$parent order by reg_name");
	if (!mysql_num_rows($parentsql))
	{	return; }
	else
	{
		while($branch = mysql_fetch_row($parentsql))
			{
			$echo_this = "<option ";
			if ($branch[0]==$lSelected)
				$echo_this.=" selected";
			$echo_this.="value=$branch[0]>";
				for ($x=1; $x<=$level; $x++)
				{
				$echo_this .=  "&nbsp;&nbsp;&nbsp;";
				}

			$echo_this.="$branch[1]</option>";

			echo $echo_this;

			$rename_level = $level;
			untreeRegion($branch[0], ++$rename_level,$input);
			}
		}
}


echo "<form method='post' action='index.php'>";
echo "<table><tr><td><b>Region name</b>:</td><td><input type='text' name='reg_name' value=''></td></tr><tr>";
echo "<td><b>This is below</b>:</td><td><select name='reg_father'><option value='0'>Top Level</option>";
$compsql = q("select reg_id, reg_name, reg_father from $lTableRegion WHERE reg_father=0 order by reg_name");
while ($companys = mysql_fetch_array($compsql))
{
echo "<option value=$companys[reg_id]";
if ($companys[reg_id]==$lSelected)
	echo " selected";
echo ">$companys[reg_name]</option>";
echo untreeRegion($companys[reg_id], 1,0);
}
echo "</select></td></tr></table>";
echo "<br><input type='submit' name='add' value='Add'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='del' value='Del'>";
echo "</form>";

?>
<form id="form">
<script language="JavaScript" type="text/javascript">
function getDate() 
{
	var myindex  = this.form.reg_1.selectedIndex;
	var SelValue = this.form.reg_1.options[myindex].value;
    document.getElementById("z").innerHTML = myindex;
}
</script>

<?



echo "<div id='z'></div>";


$lSQL="select * from $lTableRegion Where reg_father=0";
$lRes=q($lSQL);
echo "<select name='reg_1' OnChange='getDate(this.form.reg_1)'>";
while ($lRow=mysql_fetch_array($lRes))
{
	echo "<option>".$lRow["reg_name"] . "</option><br>";
	
}
echo "</select></form>";
writeCommonAdminTable("aaa","");
require("../../admfooter.php"); 
?>